%This file creates vlag, v1mkt, v2mkt, vmksh, v1sys, v2sys, vsysh, vtipmsh
%(accounting for patient size) that will be merged back to STATA for the 
%reduced-form regression 


clear
tic


vndlist=1:13;
textFileName1=sprintf('hinfo_pat.csv'); %input file: ahaid year prechs adj_vnd hsa sysid bdtot
B=dlmread(textFileName1);
B=sortrows(B,[1,2]);


textFileName11=sprintf('choice.csv'); %output file: the choice of vendors by each hospital
textFileName12=sprintf('vlag.csv'); %output file: whether each vendor was chosen in the last period
textFileName13=sprintf('v1mkt.csv'); %output file: whether each vendor is first market-dominant
textFileName14=sprintf('v2mkt.csv'); %output file: whether each vendor is second market-dominant (for tipping behavior)
textFileName15=sprintf('vmksh.csv'); %output file: the market share of each vendor
textFileName16=sprintf('vtipmsh.csv'); %output file: the difference of market share to the first-dominant vendor's share
textFileName17=sprintf('v1sys.csv'); %output file: whether each vendor is first system-dominant
textFileName18=sprintf('v2sys.csv'); %output file: whether each vendor is second system-dominant
textFileName19=sprintf('vsysh.csv'); %output file: the "share" each vendor by system members

textFileName13e=sprintf('exov1mkt.csv'); %output file: (exogenous) whether each vendor is first market-dominant
textFileName14e=sprintf('exov2mkt.csv'); %output file: (exogenous) whether each vendor is second market-dominant (for tipping behavior)
textFileName15e=sprintf('exovmksh.csv'); %output file: (exogenous) the market share of each vendor
textFileName16e=sprintf('exovtipmsh.csv'); %output file: (exogenous) the difference of market share to the first-dominant vendor's share
textFileName17e=sprintf('exov1sys.csv'); %output file: (exogenous) whether each vendor is first system-dominant
textFileName18e=sprintf('exov2sys.csv'); %output file: (exogenous) whether each vendor is second system-dominant
textFileName19e=sprintf('exovsysh.csv'); %output file: (exogenous) the "share" each vendor by system members


%### UPDATED on 11/11/2020: create bedsize-adjusted mktSH and sysSH
textFileName13_bd=sprintf('v1mktbed.csv'); %output file: whether each vendor is first market-dominant
textFileName14_bd=sprintf('v2mktbed.csv'); %output file: whether each vendor is second market-dominant (for tipping behavior)
textFileName15_bd=sprintf('vmkshbed.csv'); %output file: the market share of each vendor
textFileName16_bd=sprintf('vtipmshbed.csv'); %output file: the difference of market share to the first-dominant vendor's share
textFileName17_bd=sprintf('v1sysbed.csv'); %output file: whether each vendor is first system-dominant
textFileName18_bd=sprintf('v2sysbed.csv'); %output file: whether each vendor is second system-dominant
textFileName19_bd=sprintf('vsyshbed.csv'); %output file: the "share" each vendor by system members

textFileName13e_bd=sprintf('exov1mktbed.csv'); %output file: (exogenous) whether each vendor is first market-dominant
textFileName14e_bd=sprintf('exov2mktbed.csv'); %output file: (exogenous) whether each vendor is second market-dominant (for tipping behavior)
textFileName15e_bd=sprintf('exovmkshbed.csv'); %output file: (exogenous) the market share of each vendor
textFileName16e_bd=sprintf('exovtipmshbed.csv'); %output file: (exogenous) the difference of market share to the first-dominant vendor's share
textFileName17e_bd=sprintf('exov1sysbed.csv'); %output file: (exogenous) whether each vendor is first system-dominant
textFileName18e_bd=sprintf('exov2sysbed.csv'); %output file: (exogenous) whether each vendor is second system-dominant
textFileName19e_bd=sprintf('exovsyshbed.csv'); %output file: (exogenous) the "share" each vendor by system members



chs=zeros(size(B,1),numel(vndlist));
vlag=zeros(size(B,1),numel(vndlist));
v1mkt=zeros(size(B,1),numel(vndlist));
v2mkt=zeros(size(B,1),numel(vndlist));
vmksh=zeros(size(B,1),numel(vndlist));
%vtipmsh=zeros(size(B,1),numel(vndlist));
v1sys=zeros(size(B,1),numel(vndlist));
v2sys=zeros(size(B,1),numel(vndlist));
vsysh=zeros(size(B,1),numel(vndlist));


exov1mkt=zeros(size(B,1),numel(vndlist));
exov2mkt=zeros(size(B,1),numel(vndlist));
exovmksh=zeros(size(B,1),numel(vndlist));
%exovtipmsh=zeros(size(B,1),numel(vndlist));
exov1sys=zeros(size(B,1),numel(vndlist));
exov2sys=zeros(size(B,1),numel(vndlist));
exovsysh=zeros(size(B,1),numel(vndlist));

v1mktbed=zeros(size(B,1),numel(vndlist));
v2mktbed=zeros(size(B,1),numel(vndlist));
vmkshbed=zeros(size(B,1),numel(vndlist));
%vtipmsh=zeros(size(B,1),numel(vndlist));
v1sysbed=zeros(size(B,1),numel(vndlist));
v2sysbed=zeros(size(B,1),numel(vndlist));
vsyshbed=zeros(size(B,1),numel(vndlist));


exov1mktbed=zeros(size(B,1),numel(vndlist));
exov2mktbed=zeros(size(B,1),numel(vndlist));
exovmkshbed=zeros(size(B,1),numel(vndlist));
%exovtipmsh=zeros(size(B,1),numel(vndlist));
exov1sysbed=zeros(size(B,1),numel(vndlist));
exov2sysbed=zeros(size(B,1),numel(vndlist));
exovsyshbed=zeros(size(B,1),numel(vndlist));

%}


isv1mkt=zeros(size(B,1),1);
isv1sys=zeros(size(B,1),1);
exoisv1mkt=zeros(size(B,1),1);
exoisv1sys=zeros(size(B,1),1);

isv1mktbed=zeros(size(B,1),1);
isv1sysbed=zeros(size(B,1),1);
exoisv1mktbed=zeros(size(B,1),1);
exoisv1sysbed=zeros(size(B,1),1);


%%%^^^%%%
% ### UPDATED on 11/11/2020: This part is for NON-bedsize-adjustment
%endogenous
for i=1:size(B,1)
    vlag(i,:)=ismember(vndlist,B(i,3));
    chs(i,:)=ismember(vndlist,B(i,4));
    %compute market-related
    ms=B(B(:,5)==B(i,5)&B(:,2)==B(i,2),3);
    ms(ms==1)=[];
    if isempty(ms)==0
        occur=histc(ms,vndlist);
        occur=reshape(occur,1,[]);  
        vmksh(i,:)=occur/sum(occur);
        ms(ms==2)=[];
        ms(ms==13)=[];
        if isempty(ms)==0
          occur=histc(ms,vndlist);
          occur=reshape(occur,1,[]);      

        
          v1mkt(i,:)=ismember(vndlist,vndlist(occur==max(occur)));
          isv1mkt(i,:)=ismember(B(i,4),vndlist(occur==max(occur)));
          %v1mkt(i,2)=0;
          %v1mkt(i,13)=0;
          %vmksh(i,:)=occur/sum(occur);
          occur(occur==max(occur))=0;
          if max(occur)>0
             v2mkt(i,:)=ismember(vndlist,vndlist(occur==max(occur)));
             %v2mkt(i,2)=0;
             %v2mkt(i,13)=0;           
          end  
        end
    end
    
   %compute system-related
   if B(i,6)>0
      sys=B(B(:,6)==B(i,6)&B(:,2)==B(i,2),3);
      sys(sys==1)=[];
      if isempty(sys)==0
         occur=histc(sys,vndlist);
         occur=reshape(occur,1,[]);
         v1sys(i,:)=ismember(vndlist,vndlist(occur==max(occur)));
         isv1sys(i,:)=ismember(B(i,4),vndlist(occur==max(occur)));
         %v1sys(i,13)=0; 
         vsysh(i,:)=occur/sum(occur);
         occur(occur==max(occur))=0;
         if max(occur)>0
             v2sys(i,:)=ismember(vndlist,vndlist(occur==max(occur)));
             %v2sys(i,13)=0;           
         end               
      end
   end   
end
vtipmsh=repmat(max(vmksh,[],2),1,size(vmksh,2))-vmksh;
vlag(:,1)=0;



%exogenous
for i=1:size(B,1)
    %compute market-related
    ms=B(B(:,5)==B(i,5)&B(:,2)==B(i,2)&B(:,1)~=B(i,1),3);
    ms(ms==1)=[];
    if isempty(ms)==0
        occur=histc(ms,vndlist);
        occur=reshape(occur,1,[]);
        exovmksh(i,:)=occur/sum(occur);
        ms(ms==2)=[];
        ms(ms==13)=[];
      if isempty(ms)==0;
        occur=histc(ms,vndlist);
        occur=reshape(occur,1,[]);
        exov1mkt(i,:)=ismember(vndlist,vndlist(occur==max(occur)));
        exoisv1mkt(i,:)=ismember(B(i,4),vndlist(occur==max(occur)));
        %exov1mkt(i,2)=0;
        %exov1mkt(i,13)=0;
        %exovmksh(i,:)=occur/sum(occur);
        occur(occur==max(occur))=0;
        if max(occur)>0
           exov2mkt(i,:)=ismember(vndlist,vndlist(occur==max(occur)));
           exov2mkt(i,2)=0;
           exov2mkt(i,13)=0;           
        end 
      end
    end
    
   %compute system-related
   if B(i,6)>0
      sys=B(B(:,6)==B(i,6)&B(:,2)==B(i,2)&B(:,1)~=B(i,1),3);
      sys(sys==1)=[];
      if isempty(sys)==0
         occur=histc(sys,vndlist);
         occur=reshape(occur,1,[]);
         exov1sys(i,:)=ismember(vndlist,vndlist(occur==max(occur)));
         exoisv1sys(i,:)=ismember(B(i,4),vndlist(occur==max(occur)));
         %exov1sys(i,13)=0; 
         exovsysh(i,:)=occur/sum(occur);
         occur(occur==max(occur))=0;
         if max(occur)>0
             exov2sys(i,:)=ismember(vndlist,vndlist(occur==max(occur)));
             %exov2sys(i,13)=0;           
         end               
      end
   end   
end
exovtipmsh=repmat(max(exovmksh,[],2),1,size(exovmksh,2))-exovmksh;

%}


dlmwrite(textFileName11,[chs,B(:,1:2)],'delimiter', ',', 'precision', 8)    
dlmwrite(textFileName12,[vlag,B(:,1:2)],'delimiter', ',', 'precision', 8)    
dlmwrite(textFileName13,[v1mkt,B(:,1:2)],'delimiter', ',', 'precision', 8)    
dlmwrite(textFileName14,[v2mkt,B(:,1:2)],'delimiter', ',', 'precision', 8)    
dlmwrite(textFileName15,[vmksh,B(:,1:2)],'delimiter', ',', 'precision', 8)    
%dlmwrite(textFileName16,[vtipmsh,B(:,1:2)],'delimiter', ',', 'precision', 8)    
dlmwrite(textFileName17,[v1sys,B(:,1:2)],'delimiter', ',', 'precision', 8)    
dlmwrite(textFileName18,[v2sys,B(:,1:2)],'delimiter', ',', 'precision', 8)    
dlmwrite(textFileName19,[vsysh,B(:,1:2)],'delimiter', ',', 'precision', 8)    


dlmwrite(textFileName13e,[exov1mkt,B(:,1:2)],'delimiter', ',', 'precision', 8)    
dlmwrite(textFileName14e,[exov2mkt,B(:,1:2)],'delimiter', ',', 'precision', 8)    
dlmwrite(textFileName15e,[exovmksh,B(:,1:2)],'delimiter', ',', 'precision', 8)    
%dlmwrite(textFileName16e,[exovtipmsh,B(:,1:2)],'delimiter', ',', 'precision', 8)    
dlmwrite(textFileName17e,[exov1sys,B(:,1:2)],'delimiter', ',', 'precision', 8)    
dlmwrite(textFileName18e,[exov2sys,B(:,1:2)],'delimiter', ',', 'precision', 8)    
dlmwrite(textFileName19e,[exovsysh,B(:,1:2)],'delimiter', ',', 'precision', 8)    
%}

%%%^^^%%% END



%%%@@@%%%
% ### UPDATED on 11/11/2020: This part is for bedsize-adjustment
%EXOgenous 
for i=1:size(B,1)
    % MKT-related
    lmkt=B(B(:,5)==B(i,5)&B(:,2)==B(i,2)&B(:,1)~=B(i,1),[7,3]);  %matrix: bdtot prechs
    lmkt(lmkt(:,2)==1,:)=[]; 
    if isempty(lmkt)==0
       bvsum=zeros(numel(vndlist),1);
       for j=1:size(lmkt,1)
           bvsum(lmkt(j,2))=bvsum(lmkt(j,2))+lmkt(j,1);      
       end
       exovmkshbed(i,:)=bvsum/sum(bvsum);  
       bvsum(2)=0; %excluding self-developed because SD is not market leading
       bvsum(13)=0; %excluding "others" because "others" is not market leading
       if sum(bvsum)>0
          exov1mktbed(i,:)=ismember(vndlist,vndlist(bvsum==max(bvsum)));
          exoisv1mktbed(i,:)=ismember(B(i,4),vndlist(bvsum==max(bvsum)));
          bvsum(bvsum==max(bvsum))=0;
          if sum(bvsum)>0
             exov2mktbed(i,:)=ismember(vndlist,vndlist(bvsum==max(bvsum)));
          end     
       end
    end
    
    %SYS-related
    if B(i,6)>0
        lsys=B(B(:,6)==B(i,6)&B(:,2)==B(i,2)&B(:,1)~=B(i,1),[7,3]);  %matrix: bdtot prechs
        lsys(lsys(:,2)==1,:)=[]; 
        if isempty(lsys)==0
            sysbvsum=zeros(numel(vndlist),1);
            for j=1:size(lsys,1)
                sysbvsum(lsys(j,2))=sysbvsum(lsys(j,2))+lsys(j,1); 
            end
            exovsyshbed(i,:)=sysbvsum/sum(sysbvsum);
            exov1sysbed(i,:)=ismember(vndlist,vndlist(sysbvsum==max(sysbvsum)));
            exoisv1sysbed(i,:)=ismember(B(i,4),vndlist(sysbvsum==max(sysbvsum)));
            sysbvsum(sysbvsum==max(sysbvsum))=0;
            if sum(sysbvsum)>0
                exov2sysbed(i,:)=ismember(vndlist,vndlist(sysbvsum==max(sysbvsum)));                
            end           
        end       
    end   
end


%ENDOgenous 
for i=1:size(B,1)
    % MKT-related
    lmkt=B(B(:,5)==B(i,5)&B(:,2)==B(i,2),[7,3]);  %matrix: bdtot prechs
    lmkt(lmkt(:,2)==1,:)=[]; 
    if isempty(lmkt)==0
       bvsum=zeros(numel(vndlist),1);
       for j=1:size(lmkt,1)
           bvsum(lmkt(j,2))=bvsum(lmkt(j,2))+lmkt(j,1);      
       end
       vmkshbed(i,:)=bvsum/sum(bvsum);  
       bvsum(2)=0; %excluding self-developed because SD is not market leading
       bvsum(13)=0; %excluding "others" because "others" is not market leading
       if sum(bvsum)>0
          v1mktbed(i,:)=ismember(vndlist,vndlist(bvsum==max(bvsum)));
          isv1mktbed(i,:)=ismember(B(i,4),vndlist(bvsum==max(bvsum)));          
          bvsum(bvsum==max(bvsum))=0;
          if sum(bvsum)>0
             v2mktbed(i,:)=ismember(vndlist,vndlist(bvsum==max(bvsum)));
          end     
       end
    end
    
    %SYS-related
    if B(i,6)>0
        lsys=B(B(:,6)==B(i,6)&B(:,2)==B(i,2),[7,3]);  %matrix: bdtot prechs
        lsys(lsys(:,2)==1,:)=[]; 
        if isempty(lsys)==0
            sysbvsum=zeros(numel(vndlist),1);
            for j=1:size(lsys,1)
                sysbvsum(lsys(j,2))=sysbvsum(lsys(j,2))+lsys(j,1); 
            end
            vsyshbed(i,:)=sysbvsum/sum(sysbvsum);
            v1sysbed(i,:)=ismember(vndlist,vndlist(sysbvsum==max(sysbvsum)));
            isv1sysbed(i,:)=ismember(B(i,4),vndlist(sysbvsum==max(sysbvsum)));            
            sysbvsum(sysbvsum==max(sysbvsum))=0;
            if sum(sysbvsum)>0
                v2sysbed(i,:)=ismember(vndlist,vndlist(sysbvsum==max(sysbvsum)));                
            end           
        end       
    end   
end

dlmwrite(textFileName13_bd,[v1mktbed,B(:,1:2)],'delimiter', ',', 'precision', 8)    
dlmwrite(textFileName14_bd,[v2mktbed,B(:,1:2)],'delimiter', ',', 'precision', 8)    
dlmwrite(textFileName15_bd,[vmkshbed,B(:,1:2)],'delimiter', ',', 'precision', 8)    
%dlmwrite(textFileName16,[vtipmsh,B(:,1:2)],'delimiter', ',', 'precision', 8)    
dlmwrite(textFileName17_bd,[v1sysbed,B(:,1:2)],'delimiter', ',', 'precision', 8)    
dlmwrite(textFileName18_bd,[v2sysbed,B(:,1:2)],'delimiter', ',', 'precision', 8)    
dlmwrite(textFileName19_bd,[vsyshbed,B(:,1:2)],'delimiter', ',', 'precision', 8)    


dlmwrite(textFileName13e_bd,[exov1mktbed,B(:,1:2)],'delimiter', ',', 'precision', 8)    
dlmwrite(textFileName14e_bd,[exov2mktbed,B(:,1:2)],'delimiter', ',', 'precision', 8)    
dlmwrite(textFileName15e_bd,[exovmkshbed,B(:,1:2)],'delimiter', ',', 'precision', 8)    
%dlmwrite(textFileName16e,[exovtipmsh,B(:,1:2)],'delimiter', ',', 'precision', 8)    
dlmwrite(textFileName17e_bd,[exov1sysbed,B(:,1:2)],'delimiter', ',', 'precision', 8)    
dlmwrite(textFileName18e_bd,[exov2sysbed,B(:,1:2)],'delimiter', ',', 'precision', 8)    
dlmwrite(textFileName19e_bd,[exovsyshbed,B(:,1:2)],'delimiter', ',', 'precision', 8)    




%%%@@@%%% END




toc



